<template>
	<view class="content">
		<view>
			<view class="bluetoothConnected">
				<view class="bluetoothList" v-for="(item,index) in GET_CONNECTBLEDATA" :key="index" @tap="confirm_bluetooth(item)">
					<view class="bluetoothList-name">名称:{{item.name}}</view>
					<view class="bluetoothList-mac">地址:{{item.mac}}</view>
				</view>
			</view>
			<button type="default" @click="search_bluetooth">搜索蓝牙</button>
			<button @click="senBleLabel">打印</button>
			<view class="bluetoothItem" v-if="GET_INFODATA">
				<view class="bluetoothList" v-for="(item,index) in GET_INFODATA" :key="index" @tap="confirm_bluetooth(item)">
					<view class="bluetoothList-name">名称:{{item.name}}</view>
					<view class="bluetoothList-mac">地址:{{item.mac}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import printConnect from "@/common/print.js";  //引入打印机模板文件 
	let _this = null;
	import {
		mapGetters,
		mapActions
	} from 'vuex';
	import {
		GET_INFODATA,
		GET_CONNECTBLEDATA
	} from "@/store/gettersType.js";
	import {
		SET_CONNECTBLEDATA
	} from '@/store/actionsType.js';
	// #ifdef APP-PLUS
	const HanyinPlugin = uni.requireNativePlugin('Hanyin-Plugin'); //汉印
	// #endif
	let print;
	export default {
		data() {
			return {
				bArray: [], //用于搜索蓝牙去重用的
				no_match_list: [], //没有配对的蓝牙列表
				match_list: "", //已连接蓝牙打印机
				val: "",
				dateTimer: "",
				valArr: [],
				clearTime:''
				// 巴枪参数
				// broadcase_actions: "com.android.receive_scan_action",
				// broadcast_flag: "data"
			};
		},
		computed: {
			...mapGetters([GET_INFODATA, GET_CONNECTBLEDATA])
		},
		onShow() {
			uni.hideLoading();
			//检查是否已连接蓝牙
			// this.$check_bluetooth_connect();
			console.log('onShow');
		},
		onHide() {
			console.log('onHide');
		},
		onUnload() {
			console.log('onUnload');
		},
		onLoad(options) {
			_this = this;
			 this.$init_bluetooth();
		},
		methods: {
			...mapActions([SET_CONNECTBLEDATA]),
			senBleLabel() {
				// console.log(this.$Mock.order_data);
				if(!print) return;
				print.startPrint(this.$Mock.order_data);
			},
			// 连接打印机
			confirm_bluetooth(item) {
				let {
					name,
					mac
				} = item;
				console.log('點擊每次觸發',item)
				clearTimeout(this.clearTime); //防止多次觸發
				
				this.clearTime = setTimeout(()=>{
					// uni.getStorageSync()
					console.log('定時器觸發',item)
					let status = uni.getStorageSync('status')||true
					if(status){
						console.log('等到完成一次藍牙連接后觸發',item)
						 uni.setStorageSync('status',false)
						this.$check_bluetooth_open().then(res => {
							console.log(res);
							//进行打印机连接
							if (res) {
						        print =new printConnect(item);  //打印机连接
							}
						})
					}
					
					clearTimeout(this.clearTime); //防止多次觸發
				},300)
				//判断蓝牙是否打开
				
			},
			//搜索没匹配的蓝牙设备
			search_bluetooth(address) {
				let _this = this;
				//检查蓝牙是否开启
				this.$check_bluetooth_open().then(ores => {
					if (ores) {
						console.log(ores);
						//搜索蓝牙
						_this.$search_bluetooth().then(bres => {
							console.log(bres);
							if (bres.code) {
								_this.$search_pipei().then(pres => {
									console.log(pres);
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.bluetoothItem {
		width: 100%;
		height: 100%;
	
		.bluetoothList {
			display: flex;
			flex-direction: column;
			padding: 20rpx;
			border-bottom: 1rpx solid #BEBEBE;
			font-size: 18rpx;
		}
	}
</style>
